package com.quectel.util.kit;

import com.quectel.util.common.JacksonUtils;
import lombok.Data;
import org.apache.commons.codec.binary.Hex;
import org.junit.Test;

import java.util.Date;
import java.util.HashMap;
import java.util.concurrent.CountDownLatch;

/**
 * Kryo测试用例
 *
 * @author: caolu
 * @email: louis.cao@quectel.com
 * @date: 2023-05-04 08:18:00
 */
public class KryoKitTestCase {

    @Data
    private static final class User {
        private String name = "zhangsan";
        private Integer age = 30;
        private Date date = new Date();


    }

    /**
     * 测试字段变化
     *
     * @throws InterruptedException
     */
    @Test
    public void testFiledChange() throws Exception {
        byte[] serialize = KryoKit.serialize(new User());
        System.out.println(new String(serialize));
        System.out.println(Hex.encodeHexString(serialize));
        String hex = "0100636f6d2e7175656374656c2e7574696c2e6b69742e4b72796f4b6974546573744361736524557365f201036167e5646174e56e616de5023c01016a6176612e7574696c2e446174e501e89aceb0fe3003017a68616e677361ee";
        Object deserialize = KryoKit.deserialize(Hex.decodeHex(hex));
        System.out.println(JacksonUtils.toJsonString(deserialize));

    }


    @Test
    public void test() throws InterruptedException {
        long t1 = System.currentTimeMillis();
        CountDownLatch countDownLatch = new CountDownLatch(128);
        for (int i = 0; i < 128; i++) {
            new Thread(
                    () -> {
                        for (int j = 0; j < 1000000; j++) {
                            byte[] serialize = KryoKit.serialize(new HashMap<>());
                            KryoKit.deserialize(serialize);
                        }
                        countDownLatch.countDown();
                    }
            ).start();
        }
        countDownLatch.await();
        System.out.println(System.currentTimeMillis() - t1);

    }

    @Test
    public void test1()  {


    }
}
